package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Employee;
import com.ssh.entity.Position;
import com.ssh.manager.EmployeeManager;
import com.ssh.manager.PositionManager;
import com.ssh.manager.SellerManager;
import com.ssh.manager.impl.EmployeeManagerImpl;
import com.ssh.manager.impl.SellerManagerImpl;
@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/position/position.jsp"),
@Result(name="getAll", type="json", params={"root","positions","excludeproperties","positions\\[\\d+\\]\\.id,positions\\[\\d+\\]\\.name,positions\\[\\d+\\]\\.createDate"}),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount,page\\.numberPerPage," +
		"page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.createDate"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="update", type="json", params={"root","jr","excludeproperties","jr\\.code"})
})
public class PositionAction extends ActionSupport{
	@Resource(name="positionManagerImpl")
	private PositionManager positionManager;
	@Resource(name="sellerManagerImpl")
	private SellerManager sellerManager;
	@Resource(name="employeeManagerImpl")
	private EmployeeManager employeeManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private List<Position> positions;
	private Position position;
	private String ids;

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public String getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}

	public List<Position> getPositions() {
		return positions;
	}

	public void setPositions(List<Position> positions) {
		this.positions = positions;
	}
	
	public String getAll(){
		positions = positionManager.getAll();
		return "getAll";
	}
	
	public String goJsp(){
		return "success";
	}
	
	public String findList(){
		positionManager.findListByPage(page, isDesc);
		return "findList";
	}
	
	public String add(){
		position.setDel(0);
		position.setCreateDate(new Timestamp(System.currentTimeMillis()));
		position = positionManager.saveUpdate(position);
		return "success";
	}
	
	public String update(){
		try {
			Position p = positionManager.getById(position.getId());
			p.setName(position.getName());
			positionManager.saveUpdate(p);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "update";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					position = positionManager.getById(new Long(d[i]));
					position.setDel(1);
					positionManager.saveUpdate(position);
					//删除此职位下的所有业务员
					List<Employee> e = employeeManager.getAllByPid(position.getId());
					for (int j = 0; j < e.size(); j++) {
						e.get(j).setDel(1);
						employeeManager.saveUpdate(e.get(j));
					}
				}
			}else{
				position = positionManager.getById(position.getId());
				position.setDel(1);
				positionManager.saveUpdate(position);
				List<Employee> e = employeeManager.getAllByPid(position.getId());
				for (int j = 0; j < e.size(); j++) {
					e.get(j).setDel(1);
					employeeManager.saveUpdate(e.get(j));
				}
			}
			
			int _p = (int) page.getCurPageNum();
			page.setNumberPerPage(20);
			positionManager.findListByPage(page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "delete";
	}
	
	/**
	 * 验证职位名
	 * @return
	 */
	public String verificationName(){
		if (positionManager.verificationName(position.getName())) {
			jr.setCode("y");
		}else{
			jr.setCode("n");
		}
		return "update";
	}
}
